package com.chaoyong.higo.adapter;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseAdapter;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.utils.ViewHolder;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

/**
 * 我的账户
 * 
 * @author zk
 *
 */
public class AccountSafeAdapter extends BaseAdapter<Object> {

	private String[] itemsName = { "登录密码", "支付密码", "手机号绑定" };

	/** 选项名称 */
	private TextView item_safe_name;
	/** 类型 */
	private TextView item_safe_text;
	/** 开关 */
	private ToggleButton safe_switch_tb;
	/** 向右图片 */
	private ImageView safe_right_lv;
	private View itemView;// 间隔作用的view

	private int isOpen = 0;// 判断开关：0，开；1，关
	
	private String phone;

	public AccountSafeAdapter(Context context) {
		super(context);
		//httpGetUserPhone();
	}

	private void httpGetUserPhone() {
		// TODO Auto-generated method stub
		JSONObject info = new JSONObject();
		try {
			info.put("uid", PrefUtils.getString(mContext, "userId", ""));
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		JSONObject json = new JSONObject();
		try {
			json.put("model", "MemberInfo");
			json.put("function", "index");
			json.put("info", info);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.configCookieStore(Values.cookieStore);
		utils.send(HttpMethod.POST, OApi.Appport_appMember, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jo = new JSONObject(result);
					if(jo.getInt("status") == 1){
						JSONObject data = jo.getJSONObject("data");
						phone = data.getString("phone");
						
						item_safe_text.setText(phone.substring(0,3)+"****"+phone.substring(7));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public int getCount() {
		return 3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_account_safe, null);
		}

		findView(convertView);

		item_safe_name.setText(itemsName[position]);

		switch (position) {
		case 0:
			safe_switch_tb.setVisibility(View.GONE);
			itemView.setVisibility(View.GONE);
			item_safe_text.setVisibility(View.VISIBLE);
			safe_right_lv.setVisibility(View.VISIBLE);
			item_safe_text.setText("修改");
			break;
		/*case 1:
			safe_switch_tb.setVisibility(View.VISIBLE);
			item_safe_text.setVisibility(View.GONE);
			safe_right_lv.setVisibility(View.GONE);

			safe_switch_tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						isOpen = 0;
					} else {
						isOpen = 1;
					}

					Toast.makeText(mContext, "" + isOpen, Toast.LENGTH_SHORT).show();
				}
			});
			break;*/
		case 1:
			itemView.setVisibility(View.VISIBLE);
			item_safe_text.setVisibility(View.GONE);
			break;
		case 2:
			httpGetUserPhone();
			//String phone = PrefUtils.getString(mContext, "userPhone", "");
			/*if (!EmptyUtil.isEmpty(phone)) {
				item_safe_text.setText(phone.substring(0,3)+"****"+phone.substring(7));
			}else {
				item_safe_text.setText("");
			}*/
			break;
		/*case 4:
			item_safe_text.setText("未绑定");
			break;*/
		}

		return convertView;
	}

	private void findView(View convertView) {
		item_safe_name = ViewHolder.get(convertView, R.id.item_safe_name);
		item_safe_text = ViewHolder.get(convertView, R.id.item_safe_text);
		safe_switch_tb = ViewHolder.get(convertView, R.id.safe_switch_tb);
		safe_right_lv = ViewHolder.get(convertView, R.id.safe_right_lv);
		itemView = ViewHolder.get(convertView, R.id.item_safe_view);
	}

}
